package com.hzncc.scaffold.controller.Front.Home;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.config.MinioConfig;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.*;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.mapper.CourseMapper;
import com.hzncc.scaffold.service.*;
import com.hzncc.scaffold.utils.MinioUtil;
import com.hzncc.scaffold.utils.NumberUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-01 16:21
 * @Description: null
 */
@RestController
@Api(tags = "前台接口-首页")
@RequestMapping("/home")
public class HomeController {

    @Autowired
    NewsService newsService;

    @Autowired
    LectureService lectureService;

    @Autowired
    CourseService courseService;

    @Autowired
    UserService userService;

    @Autowired
    MinioConfig minioConfig;

    @Autowired
    ChapterService chapterService;

    @Autowired
    TaskService taskService;

    @Autowired
    CourseMapper courseMapper;


    @Value("${project.imgFileSuffix}")
    public String imgFileSuffix;

//    @GetMapping("/newsList")
//    @ApiOperation("新闻列表")
//    public ResultT newsList(Integer pageNum,Integer pageSize,String type) {
//
//        LambdaQueryWrapper<News> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(StrUtil.isNotEmpty(type), News::getType, type);
//        lqw.orderByDesc(News::getId);
//
//        if (pageSize != null && pageNum != null && pageSize > 0 && pageNum > 0) {
//            PageHelper.startPage(pageNum, pageSize);
//        }
//
//        List<News> newsList = newsService.list(lqw);
//
//
//        return ResultT.success(new PageInfo<>(newsList));
//    }

    @GetMapping("/newsList")
    @ApiOperation("新闻列表")
    public ResultT newsList(Integer pageNum,Integer page,Integer pageSize,String type) {
        Integer pageData = NumberUtils.pageNum(page, pageNum);
        ArrayList<NewsListVo> newsListVos = new ArrayList<>();

        LambdaQueryWrapper<News> lqw = new LambdaQueryWrapper<>();
        lqw.eq(News::getType, type);
        lqw.eq(News::getIsTrainingClass, 0);
        lqw.eq(News::getStatus, 1);

        lqw.orderByDesc(News::getStayAtTop);
        lqw.orderByDesc(News::getSortTime);
        lqw.orderByDesc(News::getId);

        if (pageData != null && pageSize != null && pageData > 0 && pageSize > 0) {
            PageHelper.startPage(pageData, pageSize);
        }
        List<News> newsList = newsService.list(lqw);
        for (News news : newsList) {
            NewsListVo newsListVo = BeanUtil.copyProperties(news, NewsListVo.class, "attachFile");
            JSONArray jsonArray = new JSONArray(news.getAttachFile());
            List<AttachFileVo> attachFileVoList = JSONUtil.toList(jsonArray, AttachFileVo.class);
            newsListVo.setAttachFile(attachFileVoList);

            UserVo userVo = new UserVo();
            userVo.setId(news.getPublisherId());
            userVo.setName(userService.getById(news.getPublisherId()).getName());

            String thumbnail = "";
            String content = news.getContent();
            List<String> contentTagList = StrUtil.split(content, "\"");
            ArrayList<String> newTagList = new ArrayList<>();

            for (String contentTag : contentTagList) {
                if (StrUtil.contains(contentTag, ".jpg")||StrUtil.contains(contentTag, ".png")) {
                    newTagList.add(contentTag);
                }
            }
            if (CollUtil.isNotEmpty(newTagList)) {
                thumbnail = newTagList.get(0);
            }
            newsListVo.setThumbnail(thumbnail);
            newsListVos.add(newsListVo);
        }


//        PageInfo<NewsListVo> info = new PageInfo<>();
//        info.setList(newsListVos);
//        info.setTotal(new PageInfo<>(newsList).getTotal());

        return ResultT.success(newsListVos);
    }

    @GetMapping("/lectureList")
    @ApiOperation("名师讲堂列表")
    public ResultT lectureList(Integer pageNum,Integer pageSize) {

        LambdaQueryWrapper<Lecture> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Lecture::getId);

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Lecture> lectureList = lectureService.list(lqw);

        for (Lecture lecture : lectureList) {
            if(StrUtil.isNotEmpty(lecture.getCover())){
                lecture.setCover(minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/"+ lecture.getCover());
            }
        }
//        return ResultT.success(new PageInfo<>(lectureList));
        return ResultT.success(lectureList);

    }


    @GetMapping("/courseList")
    @ApiOperation("课程列表")
    public ResultT courseList(Integer page,Integer pageSize) {
        LambdaQueryWrapper<Course> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Course::getStatus,1);
        lqw.eq(Course::getIsDelete,0);
        lqw.orderByDesc(Course::getId);

        if (page != null && pageSize != null && page > 0 && pageSize > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<Course> courseList = courseService.list(lqw);
        ArrayList<Course> data = new ArrayList<>();
        for (Course course : courseList) {
            Boolean flag = false;
            LambdaQueryWrapper<Chapter> chapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
            chapterLambdaQueryWrapper.eq(Chapter::getCourseId,course.getId());
            List<Chapter> list = chapterService.list(chapterLambdaQueryWrapper);
            if(CollUtil.isNotEmpty(list)){
                LambdaQueryWrapper<Task> taskLambdaQueryWrapper = new LambdaQueryWrapper<>();
                taskLambdaQueryWrapper.in(Task::getChapterId,list.stream().map(c -> c.getId()).collect(Collectors.toList()));
                List<Task> taskList = taskService.list(taskLambdaQueryWrapper);
                for (Task task : taskList) {
                    if(task.getType() == 2 || task.getType() == 3 || task.getType() == 4){
                        flag = true;
                    }
                }
            }
            if(StrUtil.isNotEmpty(course.getCover())){
                course.setCover(imgFileSuffix+ course.getCover());
            }
            if(!flag){
                data.add(course);
            }
        }
        return ResultT.success(data);
    }
}
